/**
 * 角色数据权限配置
 * hujun3 2018-01-08
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/roleDataPowerManager/roleDataPowerManager.js',
  './custom/widgets/js/yufpOrgTree.js',
  './custom/widgets/js/yufpExtTree.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 创建virtual model
    yufp.custom.vue({
      el: '#roleDatapower',
      data: function () {
        return {
          formdata: {},
          typeOptions: [],
          resetButton: !yufp.session.checkCtrl('reset'), // 重置选择按钮控制
          setOtherButton: !yufp.session.checkCtrl('setOther'), // 反选选择按钮控制
          copyButton: !yufp.session.checkCtrl('copy'), // 复制选择按钮控制
          uploadButton: !yufp.session.checkCtrl('upload'), // 导出选择按钮控制
          reourceUrl: backend.appOcaService + '/api/adminsmauthteco/datapowertreequery?sysId=' + yufp.session.logicSys.id,
          showObjectFlag: 'R',
          sourObjectId: '',
          filterSub: [], // 数据权限明细数据字典数据
          dialogVisible: false, // 复制dialog
          roleGrid: {
            query: {
              orgId: '',
              roleCode: '',
              roleName: ''
            },
            height: yufp.custom.viewSize().height - 155,
            currentRow: null,
            checkbox: false,
            dataUrl: backend.appOcaService + '/api/adminsmrole/querybyrolests',
            dataParams: {
              condition: JSON.stringify({
                roleSts: 'A',
                orgId: yufp.session.org.id
              })
            },
            tableColumns: [{
              label: this.$t('roleDataPowerManager.jsmc'),
              prop: 'roleName',
              resizable: true
            },
            {
              label: this.$t('roleDataPowerManager.jsdm'),
              prop: 'roleCode',
              resizable: true
            },
            {
              label: this.$t('roleDataPowerManager.ssjg'),
              prop: 'orgName',
              resizable: true
            }
            ]
          },
          menuTree: {
            loading: false,
            height: yufp.custom.viewSize().height - 130,
            treeCheckBox: true
          },
          filterText: '',
          ifTreeButton: false

        };
      },
      methods: {
        exportInfoFn: function () { // 导出数据
          var objectId = '',
            objectType = 'R';
          if (this.roleGrid.currentRow != null) {
            objectId = this.roleGrid.currentRow.roleId;
          }
          var resourceType = ['D'];
          var param = {
            objectId: objectId,
            objectType: objectType,
            resourceType: resourceType,
            sysId: yufp.session.logicSys.id
          };
          var params = {};
          params.url = backend.appOcaService + '/api/adminsmauthteco/export';
          params.url = yufp.service.getUrl(params);
          params.url += '?access_token=' + yufp.service.getToken();
          params.url += '&condition=' + encodeURI(JSON.stringify(param));
          window.open(params.url);
        },
        copyInfoFn: function () { // 复制数据
          if (this.roleGrid.currentRow == null || this.roleGrid.currentRow === '') {
            this.$message({
              message: this.$t('roleDataPowerManager.qxxzytfzdx'),
              type: 'warning'
            });
            return;
          }
          this.sourObjectId = this.roleGrid.currentRow.roleId;
          this.dialogVisible = true;
          this.$nextTick(function () {
            this.$refs.roleTable1.remoteData();
          });
        },
        copyCheck: function () { // 选择粘贴对象，进行复制
          var em = this;
          if (em.$refs.roleTable1.selections.length == 0) {
            em.$message({
              message: em.$t('roleDataPowerManager.qxxzztdx'),
              type: 'warning'
            });
            return;
          }
          var param = {
            sourObjectType: 'R',
            sourObjectId: em.sourObjectId,
            builObjectType: 'R',
            builObjectId: em.$refs.roleTable1.selections[0].roleId,
            lastChgUsr: yufp.session.userId,
            sysId: yufp.session.logicSys.id
          };
          // 发起请求
          yufp.service.request({
            method: 'POST',
            url: backend.appOcaService + '/api/adminsmauthteco/savecopydatapowerinfo',
            data: JSON.stringify(param),
            callback: function (code, message, response) {
              em.sourObjectType = '';
              em.sourObjectId = '';
              em.builObjectType = '';
              em.$message({
                message: em.$t('roleDataPowerManager.fzcg'),
                type: 'success'
              });
              em.dialogVisible = false;
            }
          });
        },
        queryRoleFn: function (model) { // 角色查询
          this.$refs.roleTable.remoteData({
            condition: JSON.stringify({
              orgId: model.orgId ? model.orgId : null,
              roleInfo: model.roleInfo ? model.roleInfo : null,
              // roleName: model.roleName ? model.roleName : null,
              roleSts: 'A'
            })
          });
        },
        resetQueryRoleFn: function () { // 角色查询重置
          this.roleGrid.query = {
            orgId: '',
            roleInfo: ''
            // roleName: ''
          };
        },
        selectRowRole: function (row) { // 角色数据选中事件方法
          if (row) {
            var me = this;
            me.filterText = '';
            this.roleGrid.currentRow = row;
            var param = {
              objectType: this.showObjectFlag,
              objectId: row.roleId,
              resType: 'D',
              sysId: yufp.session.logicSys.id
            };
            me.reSetFn(); // 重置
            me.menuTree.loading = true;
            // 发起请求
            yufp.service.request({
              method: 'GET',
              url: backend.appOcaService + '/api/adminsmauthteco/queryinfo',
              data: param,
              callback: function (code, message, response) {
                var infos = response.data;
                var keys = [];
                for (var i = 0; i < infos.length; i++) {
                  keys.push(infos[i].authresId);
                  me.$refs.menuTree.setChecked(infos[i].authresId, true, false);
                }
                me.menuTree.loading = false;
                // me.$refs.menuTree.setCheckedKeys(keys);
              }
            });
          } else {
            this.$message({
              message: this.$t('roleDataPowerManager.qxzytsj'),
              type: 'warning'
            });
            return;
          }
        },
        reSelectFn: function () { // 反选
          var checks = this.$refs.menuTree.getCheckedKeys();
          this.selectAllFn();
          for (var i = 0; i < checks.length; i++) {
            this.$refs.menuTree.setChecked(checks[i], false, false);
          }
        },
        reSetFn: function () { // 重置
          this.$refs.menuTree.setCheckedKeys([]);
        },
        // 是否选中对象 add by chenlin 20171229
        checkObjSelected: function (row) {
          if (row === null || row === '') {
            this.$message({
              message: em.$t('roleDataPowerManager.qxxzytdxjxsq'),
              type: 'warning'
            });
            return;
          }
        },
        selectAllFn: function () { // 全选
          var nodes = this.$refs.menuTree.data;
          var keys = [];
          for (var s = 0; s < nodes.length; s++) {
            keys.push(nodes[s].id);
          }
          this.$refs.menuTree.setCheckedKeys(keys);
        },
        selectCheckFn: function (node, flag, childNodes) {
          if (node.menuType === 'D' && flag) { // 如果当前选中的节点是数据权限模版，就只能单选
            var allNodes = this.$refs.menuTree.orginalData;
            var allLength = allNodes.length;
            var up = null;
            for (var s = 0; s < allLength; s++) {
              if (allNodes[s].id == node.upMenuId) {
                up = allNodes[s];
                break;
              }
            }
            this.$refs.menuTree.setChecked(up.id, false, true);
            this.$refs.menuTree.setChecked(node.id, true, false);
          } else {
            if (flag) {
              this.$refs.menuTree.setChecked(node.id, false, false);
            }
          }
        },
        saveAllInfoFn: function () {
          var em = this;
          var objectType = this.showObjectFlag;
          var objectId;
          var dataInfo = [];
          this.checkObjSelected(this.roleGrid.currentRow);
          objectId = this.roleGrid.currentRow.roleId;
          if (objectId === null || objectType === '') {
            em.$message({
              message: em.$t('roleDataPowerManager.qxzytdxsj'),
              type: 'warning'
            });
            return;
          }
          var checksNodes = this.$refs.menuTree.getCheckedNodes();
          for (var i = 0; i < checksNodes.length; i++) {
            if (checksNodes[i].menuType === 'D') {
              var data = {
                authRecoId: null,
                authobjId: objectId,
                authobjType: objectType,
                authresType: checksNodes[i].menuType,
                lastChgUsr: yufp.session.userId,
                sysId: yufp.session.logicSys.id,
                authresId: checksNodes[i].id,
                menuId: checksNodes[i].menuId
              };
              dataInfo.push(data);
            }
          }
          if (dataInfo.length > 0) {
            // 发起请求
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/adminsmauthteco/savedatapowerinfo',
              timeout: 120000,
              data: JSON.stringify(dataInfo),
              callback: function (code, message, response) {
                if (code == 0 && response.code == 0) {
                  em.$message({
                    message: em.$t('roleDataPowerManager.bccg'),
                    type: 'success'
                  });
                } else {
                  em.$message({
                    message: em.$t('roleDataPowerManager.bcsb'),
                    type: 'warning'
                  });
                }
              }
            });
          } else {
            em.$confirm(em.$t('roleDataPowerManager.cczjscgjssjsqxxsfjx'), em.$t('roleDataPowerManager.ts'), {
              confirmButtonText: em.$t('roleDataPowerManager.qd'),
              cancelButtonText: em.$t('roleDataPowerManager.qx'),
              type: 'warning',
              center: true
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmauthteco/deletedatainfo',
                data: JSON.stringify({
                  authobjId: objectId,
                  sysId: yufp.session.logicSys.id,
                  authresType: ['D']
                }),
                callback: function (code, message, response) {
                  if (code == 0 && response.code == 0) {
                    em.$message({
                      message: em.$t('roleDataPowerManager.czcg'),
                      type: 'success'
                    });
                  } else {
                    em.$message({
                      message: em.$t('roleDataPowerManager.czsb'),
                      type: 'warning'
                    });
                  }
                }
              });
            }).catch(function () {
              return;
            });
          }
        },
        // 菜单树加节点样式 add by chenlin 20171229
        renderContent: function () {
          var createElement = arguments[0];
          var type = arguments[1].data.menuType;
          return createElement('span', [
            createElement('span', {
              attrs: {
                class: 'yu-treeMenuType' + type
              }
            }, type),
            createElement('span', arguments[1].data.label)
          ]);
        },
        handleClick: function (data, checked, node) {
          if (checked) {
            var checked = this.$refs.menuTree.getCheckedNodes();
            var noChange = [];
            for (var i = 0; i < checked.length; i++) {
              var c = checked[i];
              if (c['children'].length == 0) {
                if (c['upMenuId'] != data['upMenuId']) {
                  noChange.push(c);
                }
              }
            }
            noChange.push(data);
            this.$refs.menuTree.setCheckedNodes([]);
            this.$refs.menuTree.setCheckedNodes(noChange);
            // 交叉点击节点
          }
        },
        // 树节点过滤
        filterNode: function (value, data) {
          if (!value) {
            return true;
          }
          return data.label.indexOf(value) !== -1;
        }
      },
      watch: {
        filterText: function (val) {
          val == '' ? this.ifTreeButton = false : this.ifTreeButton = true;
          this.$refs.menuTree.filter(val);
        }
      }
    });
  };
});
